package com.zhanghaijun.createCode.jira.util;

import java.net.URI;
import java.util.ArrayList;
import java.util.List;
import com.atlassian.jira.rest.client.IssueRestClient;
import com.atlassian.jira.rest.client.JiraRestClient;
import com.atlassian.jira.rest.client.NullProgressMonitor;
import com.atlassian.jira.rest.client.ProjectRestClient;
import com.atlassian.jira.rest.client.SearchRestClient;
import com.atlassian.jira.rest.client.domain.*;
import com.atlassian.jira.rest.client.internal.jersey.JerseyJiraRestClientFactory;
import com.zhanghaijun.createCode.jira.bean.JiraIssueBean;
import com.zhanghaijun.createCode.jira.bean.JiraIssueProjectBean;
import com.zhanghaijun.createCode.util.PrintUtil;
/**
 * Created by zhanghaijun on 15/7/17.
 */
public class JiraUtil {

    private static JerseyJiraRestClientFactory factory = new JerseyJiraRestClientFactory();
    private static NullProgressMonitor pm      = new NullProgressMonitor();
    private static String                      uri     = "http://jira.51.nb/";
    private static String                      user    = "monitor";
    private static String                      pwd     = "qwertypoiuyt";
    public static List<JiraIssueProjectBean> jiraAllDataCacheSource;//所有数据的缓存

    public static void getAllProjectAndIssue(){
        try{
            URI jiraServerUri = new URI(uri);
            JiraRestClient restClient = factory.createWithBasicHttpAuthentication(jiraServerUri, user, pwd);
            ProjectRestClient prc = restClient.getProjectClient();
            IssueRestClient irc = restClient.getIssueClient();
            SearchRestClient src = restClient.getSearchClient();
            //获得所有的项目列表
            Iterable<BasicProject> listbp = prc.getAllProjects(pm);
            if(listbp!=null){
                jiraAllDataCacheSource = new ArrayList<JiraIssueProjectBean>();
                for (BasicProject bp : listbp) {
                    JiraIssueProjectBean jiraIssueProjectBean = new JiraIssueProjectBean();
                    jiraIssueProjectBean.setProjectKey(bp.getKey());
                    jiraIssueProjectBean.setProjectName(bp.getName());
                    //分项目查找
                    SearchResult sr = src.searchJql("project=" + bp.getKey(), pm);
                    //遍历查找结果中的问题集合
                    Iterable<BasicIssue> listi = sr.getIssues();
                    if(listi!=null){
                        List<JiraIssueBean>  jiraIssueBeans = new ArrayList<JiraIssueBean>();
                        for (BasicIssue bi : listi) {
                            String key = bi.getKey();
                            Issue issue = irc.getIssue(bi.getKey(), pm);
                            JiraIssueBean JiraIssueBean = new JiraIssueBean();
                            BasicUser basicUser = issue.getAssignee();
                            if(basicUser!=null){
                                JiraIssueBean.setIssueAssignee(basicUser.getName());
                            }

                            JiraIssueBean.setIssueCreationDate(issue.getCreationDate());
                            JiraIssueBean.setIssueKey(issue.getKey());
                            JiraIssueBean.setIssueName(issue.getSummary());

                            BasicUser reportUser = issue.getReporter();
                            if(reportUser!=null){
                                JiraIssueBean.setIssueReporter(reportUser.getName());
                            }
                            BasicResolution basicResolution = issue.getResolution();
                            if(basicResolution!=null){
                                JiraIssueBean.setIssueResolution(basicResolution.getName());
                            }
                            BasicStatus basicStatus = issue.getStatus();
                            if(basicStatus!=null){
                                JiraIssueBean.setIssueStatus(basicStatus.getName());
                            }
                            BasicPriority basicPriority = issue.getPriority();
                            if(basicPriority!=null){
                                JiraIssueBean.setIssuePriority(issue.getPriority().getName());
                            }
                            JiraIssueBean.setOrgId(0L);
                            jiraIssueBeans.add(JiraIssueBean);
                        }
                        jiraIssueProjectBean.setIssues(jiraIssueBeans);
                    }
                    jiraAllDataCacheSource.add(jiraIssueProjectBean);
                }
            }
        }catch (Exception e){
            e.printStackTrace();
        }
    }

    public static void main(String[] args){
        Long current = System.currentTimeMillis();
        getAllProjectAndIssue();
        //PrintUtil.println(jiraAllDataCacheSource);
        Long end = System.currentTimeMillis();
        PrintUtil.println(end-current);
    }


}



